<template>
    <el-dialog
            :title="title"
            :visible.sync="isVisible"
            :width="width"
            :before-close="handleClose"
            :lock-scroll="true"
            :close-on-click-modal="false"
            custom-class="pub_dialog"
            @open="getData"
    >
        <VForm
                ref="vformComponents"
                :form="formData.form"
                :options="formData.options"
                :rules="formData.rules"
                :labelWidth="formData.labelWidth"
                :rowspan="rowspan"
                @toLoadMethods="toLoadMethods"
        ></VForm>

        <span slot="footer" class="dialog-footer">
      <el-button @click="cancel">取 消</el-button>
      <el-button type="primary" @click="sure" v-if="openType!='detail'">确 定</el-button>
    </span>
    </el-dialog>
</template>

<script>
    import { riskControlScoreDetail,
            riskControlScoreUpdate,
            riskControlScoreAdd } from '@/api/risk/riskScore'
    import { listWithPlaceType } from '@/api/system/area'
    import VForm from "@/components/VForm/index";
    export default {
        components: {
            VForm,
        },
        props: {
            width: {
                type: String,
                default: "70%",
            },
            title: {
                type: String,
                default: "详情",
            },
            id:{},
            openType:{
                type:String,
                default:"add"
            },
            dialogVisible: {
                type: Boolean,
                default: false
            }
        },
        watch: {
            'width': {
                handler(newVal, oldVal) {
                    console.log('newVal===',newVal, oldVal)
                    let w = this.width.split('%')[0]
                    if(w>=60){
                        this.rowspan = 2
                    }else{
                        this.rowspan = 1
                    }
                },
                deep: true,
                immediate: true
            },
            'areaCodeOptions':{
                handler(newVal, oldVal) {
                    this.formData.options.forEach(t=>{
                        if(t.key=='areaCode'){
                        t.option.data = newVal
                    }
                })
                },
                deep: true,
                immediate: true
            },
          'openType':{
            handler(newVal, oldVal) {
              this.formData.options.forEach(t=>{
                 t.disabled = newVal =='detail'
              })
            },
            deep: true,
            immediate: true
          }
        },
        computed: {
            isVisible: {
                get() {
                    return this.dialogVisible
                },
                set() {
                    this.close()
                }
            }
        },
        data() {
            return {
                rowspan:1,
                dict:{},
                formData: {
                    form: {
                            year:"",
                            areaCode:"",
                            areaName:"",
                            preventScore:"",
                            municipalScore:"",
                            houseScore:"",
                            trafficScore:"",
                            denseScore:"",
                            buildingScore:"",
                        },
                    options: [
                                   {
                                       title: "年份",
                                       key: "year",
                                       type: "dateYear",
                                   },
                                {
                                    title: "区域",
                                    key: "areaCode",
                                    type: "select",
                                    multiple:false,
                                    option: {
                                        data: this.areaCodeOptions,
                                        props: {
                                            key: "areaCode", // 指定key对应data的字段
                                            label: "areaName", // 指定label对应data的字段
                                            value: "areaCode", // 指定value对应data的字段
                                        },
                                    },
                                    event:"changeAreaCode"
                                },
                                   {
                                       title: "防控得分",
                                       key: "preventScore",
                                       type: "text",
                                   },
                                   {
                                       title: "市政设施得分",
                                       key: "municipalScore",
                                       type: "text",
                                   },
                                   {
                                       title: "房屋建筑得分",
                                       key: "houseScore",
                                       type: "text",
                                   },
                                   {
                                       title: "交通设施得分",
                                       key: "trafficScore",
                                       type: "text",
                                   },
                                   {
                                       title: "密集区域得分",
                                       key: "denseScore",
                                       type: "text",
                                   },
                                   {
                                       title: "在建工地得分",
                                       key: "buildingScore",
                                       type: "text",
                                   },
                    ],
                    rules: {
                        year :[
                          { required: true, message: '请选择', trigger: 'change' },
                          {max:12, message: '长度最大为12个字符', trigger: 'change'}
                        ],
                        areaCode :[
                          { required: true, message: '请选择', trigger: 'change' },
                        ],
                        preventScore :[
                          { required: true, message: '请选择', trigger: 'blur' },
                        ],
                        municipalScore :[
                          { required: true, message: '请选择', trigger: 'blur' },
                        ],
                        houseScore :[
                          { required: true, message: '请选择', trigger: 'blur' },
                        ],
                        trafficScore :[
                          { required: true, message: '请选择', trigger: 'blur' },
                        ],
                        denseScore :[
                          { required: true, message: '请选择', trigger: 'blur' },
                        ],
                        buildingScore :[
                          { required: true, message: '请选择', trigger: 'blur' },
                        ],
                    },
                    labelWidth: "140px",
                },
            // 字典
                areaCodeOptions:[],
            };
        },
        methods: {
            toLoadMethods(methods) {
              this[methods]()
            },
            changeAreaCode(){
               let item = this.areaCodeOptions.find(t=> t.areaCode == this.formData.form.areaCode)
              if(undefined != item){
                this.formData.form.areaName = item.areaName
              }
            },
            submitFormat(){
            },
            loadFormat(){
            },
            getCheckBoxData(value,option,type){
                if(type=="submit"){
                    if(value.length==0){
                        return null
                    }
                    let temp = [];
                    value.forEach(t=>{
                        let item = option.find(x=> x.label == t)
                    if(item!=undefined){
                        temp.push(item.value)
                    }
                })
                    return temp.join(",")
                }else {
                    if(!value){
                        return []
                    }else {
                        let data = value.split(",")
                        let temp = []
                        data.forEach(t=>{
                            let item = option.find(x=> x.value == t)
                        if(item!=undefined){
                            temp.push(item.label)
                        }
                    })
                        return temp
                    }
                }
            },
            sure(){
                console.log('sure===',this.formData.form)
                // 市类型
                this.formData.form.type = 1
                this.$refs.vformComponents.$refs.form.validate().then(res=>{
                    this.submitFormat()
                    if(this.openType == "add"){
                        this.add()
                    }else{
                        this.update()
                    }
                })
            },
            add(){
                this.formData.form.id = null
                riskControlScoreAdd(this.formData.form).then(res=>{
                    if(res.code==200){
                    this.$message({
                        message: this.$t('tips.createSuccess'),
                        type: 'success'
                    })
                    this.closeSuccess()
                    }else{
                    this.$message({
                        message: res.message,
                        type: 'error'
                    })
                    }
                })
            },
            update(){
                   riskControlScoreUpdate(this.formData.form).then(res=>{
                        if(res.code==200){
                        this.$message({
                             message: this.$t('tips.updateSuccess'),
                            type: 'success'
                        })
                        this.closeSuccess()
                      }else{
                        this.$message({
                            message:  res.message,
                            type: 'error'
                        })
                     }
                })
            },
            cancel(){
                this.close()
            },
            handleClose(done) {
                done()
                this.close()
            },
            closeSuccess(){
                this.$refs.vformComponents.$refs.form.resetFields();
                this.$emit('success')
            },
            close(){
                this.$refs.vformComponents.$refs.form.resetFields();
                this.$emit('close')
            },
            getData(){
              listWithPlaceType({leftAreaCode: this.$store.state.account.user.deptIds,
                placeType: this.$commonJs.getPlaceType(this.$store.state.account.user.deptIds)}).then(res=>{
                if(res.code==200){
                  this.areaCodeOptions = res.data
                }
              })
                if(this.openType!="add"){
                    riskControlScoreDetail(this.id).then(res=>{
                        if(res.code==200){
                            this.formData.form = res.data
                            this.loadFormat()
                         }else{
                            this.$message({
                                message: this.$t('tips.systemException'),
                                type: 'error'
                            })
                        }
                    })
                }
            }
        },
    };
</script>

<style lang="scss" scoped>

</style>
